# This is a Makefile for my Google App Engine Go SDK project
# It is in the src directory but is designed to be in the src's parent - so it should be symlinked to there

# TODO add a release recipe that uses googlecode_upload.py to push new packages to project site downloads

PROJECT := SlackBuilds
SLACKBUILD := google-appengine-gosdk
VERSION := 1.7.1
BUILD   := 3
CWD     := $(shell pwd)
SOURCES := README doinst.sh gae.csh gae.sh go.csh go.sh $(SLACKBUILD).SlackBuild $(SLACKBUILD).info slack-desc
SVNURL  := https://schultzters-packages.googlecode.com/svn
SBOURL  := http://slackbuilds.org/cgit/slackbuilds/plain/development/$(SLACKBUILD)/
DLURL   := http://googleappengine.googlecode.com/files/
SRC32   := go_appengine_sdk_linux_386-$(VERSION).zip
SRC64   := go_appengine_sdk_linux_amd64-$(VERSION).zip
MD532   := $(firstword $(shell md5sum lib/$(SRC32)))
MD564   := $(firstword $(shell md5sum lib/$(SRC64)))

.PHONY: ci co tag update getlib getsbo clean build test dist release scrub
.DEFAULT: build

VPATH = src

# Check-in the current code, will prompt for commit message
ci :
	svn commit src

# Get the latest code from the repository
co :
	svn checkout $(SVNURL)/trunk/$(SLACKBUILD)/ src 

# Tag the current release, will be prompted for tag message
tag :
	svn copy $(SVNURL)/trunk/$(SLACKBUILD) \
	 $(SVNURL)/tags/$(SLACKBUILD)/release_$(VERSION)-$(BUILD) \
	 --parents

# Work on a new version, so sync to repository, get the latest SlackBuild files, and the latest tarball
# TODO test this to make sure it works then get rid of getlib and getsbo
update : 
	svn update src
	wget -NP sbo $(addprefix $(SBOURL), $(SOURCES))
	wget -NP lib $(DLURL)$(SRC32)
	wget -NP lib $(DLURL)$(SRC64)

# Download the tarball
# TODO this didn't seem to work, is it working?!
getlib :
	wget -N -P lib $(DLURL)$(SRC32)
	wget -N -P lib $(DLURL)$(SRC64)

# If the code has an official or public repository get that before continuing and if anything has changed
getsbo : 
	wget -NP sbo $(addprefix $(SBOURL), $(SOURCES))

# Clean-up any messes I may have caused
clean :
	rm -rf dist/*
	rm -rf build/*

# Build the SlackBuild 
build : clean
	sed -i -e 's/^VERSION=..*/VERSION=\$${VERSION:-$(VERSION)}/' src/$(SLACKBUILD).SlackBuild
	sed -i -e 's/^BUILD=..*/BUILD=\$${BUILD:-$(BUILD)}/' src/$(SLACKBUILD).SlackBuild
	sed -i -e 's/^VERSION=..*/VERSION="$(VERSION)"/' src/$(SLACKBUILD).info 
	sed -i -e 's/^DOWNLOAD=..*/DOWNLOAD="$(subst /,\/,$(DLURL))$(SRC32)"/' src/$(SLACKBUILD).info
	sed -i -e 's/^DOWNLOAD_x86_64=..*/DOWNLOAD_x86_64="$(subst /,\/,$(DLURL))$(SRC32)"/' src/$(SLACKBUILD).info
	sed -i -e 's/^MD5SUM=..*/MD5SUM="$(MD532)"/' src/$(SLACKBUILD).info
	sed -i -e 's/^MD5SUM_x86_64=..*/MD5SUM_x86_64="$(MD564)"/' src/$(SLACKBUILD).info	
	mkdir build/$(SLACKBUILD)
	cp -u $(addprefix src/,$(SOURCES)) build/$(SLACKBUILD)

# Test the application before distributing it
test : build
	cp -u build/$(SLACKBUILD)/* tmp
	ln lib/$(SRC32) tmp/$(SRC32)
	ln lib/$(SRC64) tmp/$(SRC64)

# Build the SlackBuild then build the schultzter package
# This actually needs be run as root because the SlackBuild uses chown root:root but that results in build & dist files being owned by root:root
dist : build
	tar -C build -czf dist/$(SLACKBUILD).tar.gz $(SLACKBUILD)
	ln lib/$(SRC32) build/$(SLACKBUILD)/$(SRC32)
	ln lib/$(SRC64) build/$(SLACKBUILD)/$(SRC64)
	cd build/$(SLACKBUILD); BUILD=$(BUILD) TAG=schultzter ARCH=i486   GARCH=386   TMP=$(CWD)/tmp OUTPUT=$(CWD)/dist ./$(SLACKBUILD).SlackBuild
	cd build/$(SLACKBUILD); BUILD=$(BUILD) TAG=schultzter ARCH=x86_64 GARCH=amd64 TMP=$(CWD)/tmp OUTPUT=$(CWD)/dist ./$(SLACKBUILD).SlackBuild
	chown -R eric:users --from=root:root .

release : 
	googlecode_upload.py -s "The Google Appengine Go SDK 32-bit version $(VERSION)" -p schultzters-packages \
	                     -l Featured,Type-Package,SlackVer-current,SlackArch-i486                           \
	                     dist/$(SLACKBUILD)-$(VERSION)-i486-$(BUILD)schultzter.tgz
	googlecode_upload.py -s "The Google Appengine Go SDK 64-bit version $(VERSION)" -p schultzters-packages \
	                     -l Featured,Type-Package,SlackVer-current,SlackArch-i486                           \
	                     dist/$(SLACKBUILD)-$(VERSION)-x86_64-$(BUILD)schultzter.tgz
	                     
# Really clean-up everything!
scrub : clean
	rm -rf sbo/*
	rm -rf src/*~
	rm -rf tmp/*
